package top.btcmarket.common.web;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.servlet.HandlerMapping;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import top.btcmarket.common.constant.CommonConst;

public class GlobalInterceptor extends HandlerInterceptorAdapter {

	@Override
	public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
			throws Exception {
		// TODO Auto-generated method stub
		
		
		 Cookie[] cookies = request.getCookies();
		 if(cookies != null && cookies.length>0){
			 for (Cookie cookie : cookies) {
					System.out.println("---------GlobalInterceptor--------"+cookie.getName()+"--------"+cookie.getValue());
				}
		 }
		 
		 
		 String zuul = request.getHeader(CommonConst.TOKEN_ZUUL_ID);
		 System.out.println("--------GlobalInterceptor---------zuul----------"+zuul);
	                
		 
		//权限的 加入， 可以再 系统启动的时候， 自动 调用 资源服务 接口，注册上去 （开发环境）
		
		String token =  (String)request.getParameter("token");
		 
		 System.out.println("GlobalInterceptor,token is "+token);
		
		 // 通过 token 从redis 里面 找到  对应的  uri 权限，和 method ， 进行匹配校验
		 String pattern = (String)
	        		request.getAttribute(HandlerMapping.BEST_MATCHING_PATTERN_ATTRIBUTE);  
		 
		 String method11 = request.getMethod();
		 
		 System.out.println("GlobalInterceptor,pattern+---------"+pattern+",method:"+method11);
		 
		return super.preHandle(request, response, handler);
	}

	@Override
	public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler,
			ModelAndView modelAndView) throws Exception {
		// TODO Auto-generated method stub
		super.postHandle(request, response, handler, modelAndView);
	}

	@Override
	public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex)
			throws Exception {
		// TODO Auto-generated method stub
		super.afterCompletion(request, response, handler, ex);
	}
	
	
}
